<?php

$GLOBALS['_DATA_'] = [];

function js($A = _A_, $C = _C_, $M = _M_)
{
    if (file_exists(__DIR__ . "/../../public/js/" . $M . "/" . $C . "/" . $A . ".js")) {
        return "<script src=/js/" . $M . "/" . $C . "/" . $A . ".js></script>";
    }
}

function user()
{
    return \Auth::user();
}
function post($param = null)
{
    if ($param == null)
        $param == "";
    if ($param == '?') {
        return request()->isMethod('post');
    } else
        return request()->post($param);
}
function get($param = null)
{
    if ($param == '?') {
        return request()->isMethod('get');
    } else
        return request()->get($param);
}

function setdata($data = [])
{
    $GLOBALS['_DATA_'] = array_merge($GLOBALS['_DATA_'], $data);
}

function dict($sign = '')
{
    return \App\Models\Category::where("sign", $sign)->with("dicts")->first()->dicts;
}
function begin()
{
    \Illuminate\Support\Facades\DB::beginTransaction();
}
function commit()
{
    \Illuminate\Support\Facades\DB::commit();
}
function rollback()
{
    \Illuminate\Support\Facades\DB::rollBack();
}
function css($A = _A_, $C = _C_, $M = _M_)
{
    if (file_exists(__DIR__ . "/../../public/css/" . $M . "/" . $C . "/" . $A . ".css")) {
        return "<link rel=\"stylesheet\" href=/css/" . $M . "/" . $C . "/" . $A . ".css>";
    }
}
//读取表格配置文件
function F($A = _A_, $C = _C_, $M = _M_)
{
    if (file_exists(__DIR__ . "/../Http/Config/" . $M . "/" . $C . "/" . $A . ".php")) {
        return (include __DIR__ . "/../Http/Config/" . $M . "/" . $C . "/" . $A . ".php");
    } else return [];
}
function throwError($msg)
{
    try {
        throw new Exception($msg);
    } catch (Exception $e) {
        exit(json_encode(error($e->getMessage())));
    }
}
//success,info,error,warning,question
function success($msg = _A_, $data = '')
{
    return ['data' => $data, 'message' => $msg, 'success' => 'success'];
}
function error($msg = _A_, $data = '')
{
    return ['data' => $data, 'message' => $msg, 'success' => 'error'];
}
function warning($msg = _A_, $data = '')
{
    return ['data' => $data, 'message' => $msg, 'success' => 'warning'];
}
function question($msg = _A_, $data = '')
{
    return ['data' => $data, 'message' => $msg, 'success' => 'question'];
}

function unsetNull($arr)
{
    if ($arr !== null) {
        if (is_array($arr)) {
            if (!empty($arr)) {
                foreach ($arr as $key => $value) {
                    if ($value === null) {
                        $arr[$key] = '';
                    } else {
                        $arr[$key] = unsetNull($value);      //递归再去执行
                    }
                }
            } else {
                $arr = '';
            }
        } else {
            if ($arr === null) {
                $arr = '';
            }         //注意三个等号
        }
    } else {
        $arr = '';
    }
    return $arr;
}


function findAllChildIdArray($needarray, $parentids, $parentidstr = "parentid", $idstr = "id")
{
    $arrayfun = [];
    foreach ($parentids as $k => $v)
        $arrayfun = array_merge($arrayfun, findAllChildIdunset($needarray, $v, $parentidstr, $idstr));
    return $arrayfun;
}

function findAllChildIdunset(&$needarray, $parentid, $parentidstr = "parentid", $idstr = "id")
{
    $arrayfun[] = $parentid;
    // dump($needarray);
    foreach ($needarray as $key => $val) {
        if ($val[$parentidstr] == $parentid) {
            unset($needarray[$key]);
            $arrayfun = array_merge($arrayfun, findAllChildIdunset($needarray, $val[$idstr], $parentidstr, $idstr));
        }
    }
    return $arrayfun;
}
//初始化方法
function makeAction()
{

    if (class_exists("App\Http\Controllers\\" . _M_ . "\\" . _C_)) {
        $class = \App::make("App\Http\Controllers\\" . _M_ . "\\" . _C_);
        if (method_exists($class, _A_)) {
            // dd(Auth::user()->getRoles());
            $where = ['model' => _M_, 'controller' => _C_, "action" => _A_];
            if (!\App\Models\RoleBasedAccessControl::where($where)->exists())
                \App\Models\RoleBasedAccessControl::insert($where);
            return \App::call([$class, _A_]);
        }
    }
    abort("404");
}
